package com.eqdd.yiqidian.ui.gongneng.renshi.train

import android.support.v4.content.ContextCompat
import android.support.v7.widget.LinearLayoutManager
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.lzy.okgo.OkGo
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*
import org.jetbrains.anko.startActivity
import java.util.*

/**
 * Created by jhj on 18-9-11.
 */
class TrainCourseMatchActivity : BaseActivity() {
    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview_refresh)
        initTopTitleBar(View.VISIBLE, "课程匹配的需求")
    }

    override fun initData() {
        var pageNum = 0
        val list = arrayListOf<ItemBean>()
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerLineItemDecoration(this))
        val slimAdapterEx = SlimAdapterEx.create()
                .register<ItemBean>(R.layout.list_item_187, SlimInjector<ItemBean> { data, injector ->
                    val str = data?.thetheme + "【" + data?.company + "】"
                    val span = SpannableString(str)
                    span.setSpan(RelativeSizeSpan(0.8f), data?.thetheme?.length
                            ?: 0, str.length, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE)
                    span.setSpan(ForegroundColorSpan(ContextCompat.getColor(this, R.color.hint_color)), data?.thetheme?.length
                            ?: 0, str.length, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE)
                    injector
                            ?.text(R.id.tv_name, span)
                            ?.text(R.id.tv_position, "培训地点：" + data?.theplace)
                            ?.text(R.id.tv_time, "预计时间：" + data?.thedateStart + "～" + data?.thedateEnd)
                })
                .attachTo(recyclerView)
                .updateData(list as List<*>?) as SlimAdapterEx<ItemBean>?
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener { recyclerView, position, v ->
                    val data = slimAdapterEx?.getDataItem(position)
                    startActivity<TrainRequestDetailActivity>(Config.ID to data?.Id)
                }
        val id = intent.getIntExtra(Config.ID, -1)
        val mvcHelper = MVCCoolHelper<List<ItemBean>>(coolRefreshView)
        val dataSource = ModelRx2DataSource<ItemBean>(ModelRx2DataSource.OnLoadSource<ItemBean> { page, register ->
            if (page == 1) {
                pageNum = 0
            }
            OkGo.post<HttpResult<List<ItemBean>>>(HttpConfig.BASE_URL + HttpConfig.TRAIN_REQUEST_COURSE_METCH)
                    .params("courseId", id)
                    .params("page", pageNum)
                    .converter(object : JsonConverter<HttpResult<List<ItemBean>>>() {
                        override fun test() {
                            super.test()
                        }
                    })
                    .adapt(FlowableBody<HttpResult<List<ItemBean>>>())
                    .flatMap({ result ->
                        if (page == 1) {
                            mvcHelper.isAutoLoadMore = true
                        }
                        pageNum = result.page
                        Flowable.just<List<ItemBean>>(result.items ?: ArrayList())
                    })


        }, 10)

        mvcHelper.setDataSource(dataSource)
        mvcHelper.adapter = slimAdapterEx
        mvcHelper.refresh()
    }

    override fun setView() {
    }

    data class ItemBean(
            val Id: Int = 0,
            val companyId: Int = 0,
            val thecategory: String = "",
            val thetheme: String = "",
            val thedateStart: String = "",
            val thedateEnd: String = "",
            val company: String = "",
            val theplace: String = "",
            val createTime: String = ""
    )
}